package com.xiaozhi.controller;

import com.xiaozhi.component.ChatMetrics;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 性能监控控制器
 * 提供聊天性能统计信息的查询接口
 */
@Tag(name = "性能监控")
@RestController
@RequestMapping("/api/metrics")
@Slf4j
public class MetricsController {
    
    @Autowired
    private ChatMetrics chatMetrics;
    
    @Operation(summary = "获取聊天性能统计", description = "获取详细的聊天性能统计信息")
    @GetMapping("/chat")
    public ResponseEntity<Map<String, Object>> getChatMetrics() {
        log.info("获取聊天性能统计信息");
        
        try {
            Map<String, Object> metrics = chatMetrics.getMetrics();
            return ResponseEntity.ok(metrics);
        } catch (Exception e) {
            log.error("获取性能统计失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    @Operation(summary = "获取性能摘要", description = "获取简化的性能摘要信息")
    @GetMapping("/chat/summary")
    public ResponseEntity<String> getChatMetricsSummary() {
        log.info("获取聊天性能摘要");
        
        try {
            String summary = chatMetrics.getMetricsSummary();
            return ResponseEntity.ok(summary);
        } catch (Exception e) {
            log.error("获取性能摘要失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    @Operation(summary = "重置性能统计", description = "清空所有性能统计数据")
    @PostMapping("/chat/reset")
    public ResponseEntity<String> resetChatMetrics() {
        log.info("重置聊天性能统计");
        
        try {
            chatMetrics.reset();
            return ResponseEntity.ok("性能统计已重置");
        } catch (Exception e) {
            log.error("重置性能统计失败: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError().build();
        }
    }
}